package cn.tedu.wenzhang3.controller;


import cn.tedu.wenzhang3.base.response.JsonResult;
import cn.tedu.wenzhang3.mapper.ArticleMapper;
import cn.tedu.wenzhang3.mapper.UserMapper;
import cn.tedu.wenzhang3.pojo.dto.AddArticle;
import cn.tedu.wenzhang3.pojo.entity.Article;
import cn.tedu.wenzhang3.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("v1/article/")
@Api(tags = "文章模块")
@Validated
public class ArticleController {
    @Autowired(required = false)
    private ArticleMapper articleMapper;



    @PostMapping("AddArticle")
    @ApiOperation(value = "发布文章")
    public JsonResult AddArticle(@RequestBody @Validated AddArticle addArticle, @ApiIgnore HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");
        Article article=new Article();
        BeanUtils.copyProperties(addArticle,article);
        article.setUserId(userVO.getId());
        int i = articleMapper.insertArticle(article);
        if (i==1)
            return JsonResult.ok();
        return JsonResult.notok();
    }


    @GetMapping("selectAllArticle")
    @ApiOperation(value = "获取所有的文章")
    public JsonResult selectAllArticle(){
        return JsonResult.ok(articleMapper.selectAllArticle());
    }



    @GetMapping("selectByUidArticle")
    @ApiOperation(value = "根据用户ID获取文章")
    @ApiImplicitParam(name = "id",required = true,dataType = "long",example = "1")
    public JsonResult selectByUidArticle(@NotNull(message = "id不能为空") @RequestParam Long id){
        return JsonResult.ok(articleMapper.selectByUidArticle(id));
    }

    @GetMapping("selectMyIdArticle")
    @ApiOperation(value = "获取自己的所有文章")
    public JsonResult selectByMidArticle(@ApiIgnore HttpSession session){
        Long id=((UserVO)session.getAttribute("user")).getId();
        return JsonResult.ok(articleMapper.selectByUidArticle(id));
    }
}
